package com.crazy.jiakoulachang.controller;

import com.crazy.jiakoulachang.annotation.AuthCheck;
import com.crazy.jiakoulachang.annotation.MyLog;
import com.crazy.jiakoulachang.common.ApiRestResponse;
import com.crazy.jiakoulachang.exception.ExceptionEnum;
import com.crazy.jiakoulachang.service.ProductService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: 卢辉宇
 * @date: 2023/3/15 21:46
 * @description:
 */
@RestController
@RequestMapping("/product")
@Api(tags = "订单控制类")
public class ProductController {
    @Resource
    ProductService productService;

    @MyLog(module = "订单",operator = "测试接口")
    @AuthCheck(anyRole = {"user","admin"})
    @GetMapping("/test")
    public String test(String str,Integer i){
        System.out.println("调用测试接口！！！"+str+i);
        return "调用测试接口！！！"+str+i;
    }

    /**
     *
     * @param userName
     * @param number
     * @param productType
     * @param weight
     * @param details @RequestParam(required = false)可传可不传
     * @return
     */
    @AuthCheck
    @MyLog(module = "订单",operator = "添加订单接口")
    @ApiOperation("添加订单接口")
    @PostMapping("/add")
    public ApiRestResponse add(String userName,@RequestParam(required = false) String number,String productType,Double weight,@RequestParam(required = false) String details){
        productService.add(userName, number, productType, weight,details);

        return ApiRestResponse.success();
    }

    @AuthCheck
    @MyLog(module = "订单",operator = "零售")
    @PostMapping("/sale")
    public ApiRestResponse sale(String productType,Double weight,Double price,@RequestParam(required = false) String details){
        productService.sale(productType, weight,price,details);

        return ApiRestResponse.success();
    }

    @AuthCheck
    @MyLog(module = "订单",operator = "删除订单")
    @PostMapping("/delete")
    public ApiRestResponse delete(Integer productId){
        productService.deleteByProductId(productId);
        return ApiRestResponse.success();
    }

    @AuthCheck
    @MyLog(module = "订单",operator = "修改订单")
    @PostMapping("/update")
    public ApiRestResponse update(Integer productId,String userName,@RequestParam(required = false) String number,String productType,Double weight,@RequestParam(required = false) Double netReceipts,@RequestParam(required = false) String details){
        productService.updateProductAndUser(productId,userName,number,productType,weight,netReceipts,details);

        return ApiRestResponse.success();
    }

    @AuthCheck
    @MyLog(module = "订单",operator = "修改零售")
    @PostMapping("/saleEdit")
    public ApiRestResponse saleEdit(Integer productId,String productType,Double weight,Double price,@RequestParam(required = false) Double netReceipts,@RequestParam(required = false) String details){
        productService.saleEdit(productId,productType,weight,price,netReceipts,details);

        return ApiRestResponse.success();
    }

    @AuthCheck
    @GetMapping("/getPage")
    public ApiRestResponse getPage(@RequestParam(defaultValue = "1") int currentPage,
                                   @RequestParam(defaultValue = "9999") int pageSize){
        try {
            PageInfo page = productService.getPage(currentPage, pageSize);
//            System.out.println(page);
            return ApiRestResponse.success(page);
        }catch (Exception e){
            e.printStackTrace();
        }

        return ApiRestResponse.error(ExceptionEnum.PAGE_FAIL);
    }

    @AuthCheck
    @GetMapping("/selectNotPay")
    public ApiRestResponse selectNotPay(@RequestParam(defaultValue = "1") int currentPage,
                                   @RequestParam(defaultValue = "99999") int pageSize){
        try {
            PageInfo page = productService.selectNotPay(currentPage, pageSize);
//            System.out.println(page);
            return ApiRestResponse.success(page);
        }catch (Exception e){
            e.printStackTrace();
        }

        return ApiRestResponse.error(ExceptionEnum.PAGE_FAIL);
    }

    @AuthCheck
    @GetMapping("/selectByCreateTime")
    public ApiRestResponse selectByCreateTime(@RequestParam(defaultValue = "1") int currentPage,
                                   @RequestParam(defaultValue = "99999") int pageSize,String startTime,String endTime){
        try {
            PageInfo page = productService.selectByCreateTime(currentPage, pageSize,startTime,endTime);
//            System.out.println(page);
            return ApiRestResponse.success(page);
        }catch (Exception e){
            e.printStackTrace();
        }

        return ApiRestResponse.error(ExceptionEnum.PAGE_FAIL);
    }

    /**
     * 根据开始和结束时间查询实收入
     * @param startTime
     * @param endTime
     * @return
     */
    @AuthCheck
    @GetMapping("/selectGeneralIncomeByCreateTime")
    public ApiRestResponse selectGeneralIncomeByCreateTime(String startTime,String endTime){
        Double generalIncome = productService.selectGeneralIncomeByCreateTime(startTime, endTime);
        return ApiRestResponse.success(generalIncome);
    }

    /**
     * 导出未付款的数据到excel
     * @param response
     */
    @MyLog(module = "订单",operator = "导出未付款的数据到excel")
    @AuthCheck
    @GetMapping("/exportData")
    public ApiRestResponse exportData(HttpServletResponse response) throws IOException {
        productService.exportData(response);
        return ApiRestResponse.success();
    }

    /**
     * 导出数据到excel
     * @param response
     */
    @AuthCheck
    @GetMapping("/downByCreateTime")
    public ApiRestResponse downByCreateTime(HttpServletResponse response,String startTime,String endTime) throws IOException {
        productService.downByCreateTime(response,startTime,endTime);
        return ApiRestResponse.success();
    }


}
